package cn.iocoder.yudao.module.polling.service.pollingtask;

import cn.iocoder.yudao.module.polling.controller.admin.pollingtask.vo.PollingTaskCreateReqVO;
import cn.iocoder.yudao.module.polling.controller.admin.pollingtask.vo.PollingTaskExportReqVO;
import cn.iocoder.yudao.module.polling.controller.admin.pollingtask.vo.PollingTaskPageReqVO;
import cn.iocoder.yudao.module.polling.controller.admin.pollingtask.vo.PollingTaskUpdateReqVO;
import cn.iocoder.yudao.module.polling.convert.pollingtask.PollingTaskConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingtask.PollingTaskDO;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingtask.PollingTaskMapper;
import cn.iocoder.yudao.module.polling.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 巡检任务 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class PollingTaskServiceImpl implements PollingTaskService {

    @Resource
    private PollingTaskMapper pollingTaskMapper;

    @Override
    public Integer createPollingTask(PollingTaskCreateReqVO createReqVO) {
        // 插入
        PollingTaskDO pollingTask = PollingTaskConvert.INSTANCE.convert(createReqVO);
        pollingTaskMapper.insert(pollingTask);
        // 返回
        return pollingTask.getId();
    }

    @Override
    public void updatePollingTask(PollingTaskUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePollingTaskExists(updateReqVO.getId());
        // 更新
        PollingTaskDO updateObj = PollingTaskConvert.INSTANCE.convert(updateReqVO);
        pollingTaskMapper.updateById(updateObj);
    }

    @Override
    public void deletePollingTask(Integer id) {
        // 校验存在
        this.validatePollingTaskExists(id);
        // 删除
        pollingTaskMapper.deleteById(id);
    }

    private void validatePollingTaskExists(Integer id) {
        if (pollingTaskMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.STANDARD_NOT_EXISTS);
        }
    }

    @Override
    public PollingTaskDO getPollingTask(Integer id) {
        return pollingTaskMapper.selectById(id);
    }

    @Override
    public List<PollingTaskDO> getPollingTaskList(Collection<Integer> ids) {
        return pollingTaskMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PollingTaskDO> getPollingTaskPage(PollingTaskPageReqVO pageReqVO) {
        return pollingTaskMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PollingTaskDO> getPollingTaskList(PollingTaskExportReqVO exportReqVO) {
        return pollingTaskMapper.selectList(exportReqVO);
    }

}
